<script setup lang="ts">
import {watch, ref, computed} from 'vue'
import {TestCase} from "../../../../../generated";
import {QuestionType} from "../types/questionTypes";
import ProgrammingTest from "./ProgrammingTest.vue";
import MultipleChoiceTest from "./MultipleChoiceTest.vue";
import FillBlankTest from "./FillBlankTest.vue";
import ComprehensiveTest from "./ComprehensiveTest.vue";
import JudgementTest from "./JudgementTest.vue";

const props = defineProps<{
  testCase?: TestCase[]
  isEditorFullscreen?: boolean
  questionType?: number  // 接收题目类型属性
  options?: Array<{option?: string, isCorrect?: number, analysis?: string}>
  isCorrect?: number
  analysis?: string
  answers?: string[]
  referenceAnswer?: string
  score?: number
}>()

const emit = defineEmits<{
  'update:testCase': [value: TestCase[]]
  'generate:testCase': []
}>()

// 创建测试数据对象，用于传递给各组件
const testCaseData = ref({
  choiceOptionList: (props.options || []).map(opt => ({
    option: opt.option || '',
    isCorrect: opt.isCorrect || 0,
    analysis: opt.analysis || ''
  })),
  isCorrect: props.isCorrect || 0,
  analysis: props.analysis || '',
  answers: props.answers || [],
  referenceAnswer: props.referenceAnswer || '',
  score: props.score || 0
})

// 监听传入数据的变化
watch([
  () => props.options,
  () => props.isCorrect, 
  () => props.analysis,
  () => props.answers,
  () => props.referenceAnswer,
  () => props.score
], ([options, isCorrect, analysis, answers, referenceAnswer, score]) => {
  testCaseData.value = {
    choiceOptionList: options ? options.map(opt => ({
      option: opt.option || '',
      isCorrect: opt.isCorrect ?? 0,
      analysis: opt.analysis || ''
    })) : testCaseData.value.choiceOptionList,
    isCorrect: isCorrect ?? 0,
    analysis: analysis || testCaseData.value.analysis,
    answers: answers || testCaseData.value.answers,
    referenceAnswer: referenceAnswer || testCaseData.value.referenceAnswer,
    score: score ?? testCaseData.value.score
  }
})

// 引用子组件实例
const programmingTestRef = ref()
const multipleChoiceTestRef = ref()
const judgementTestRef = ref()
const fillBlankTestRef = ref()
const comprehensiveTestRef = ref()

// 添加生成测试用例状态控制
const isGeneratingTestCase = ref(false)

// 根据传入的questionType显示相应内容
const currentQuestionType = computed(() => props.questionType || QuestionType.PROGRAMMING)

// 显示编程题测试用例相关内容
const showProgrammingContent = computed(() => {
  return currentQuestionType.value === QuestionType.PROGRAMMING
})

// 显示选择题相关内容
const showMultipleChoiceContent = computed(() => {
  return currentQuestionType.value === QuestionType.MULTIPLE_CHOICE
})

// 显示判断题相关内容
const showJudgementContent = computed(() => {
  return currentQuestionType.value === QuestionType.JUDGEMENT
})

// 显示填空题相关内容
const showFillBlankContent = computed(() => {
  return currentQuestionType.value === QuestionType.FILL_BLANK
})

// 显示综合题相关内容
const showComprehensiveContent = computed(() => {
  return currentQuestionType.value === QuestionType.COMPREHENSIVE
})

// 处理生成测试用例
const handleGenerateTestCase = () => {
  isGeneratingTestCase.value = true
  emit('generate:testCase')
}

// 重置生成测试用例状态
const resetGeneratingTestCase = () => {
  isGeneratingTestCase.value = false
  // 确保子组件也重置状态
  if (programmingTestRef.value) {
    programmingTestRef.value.resetGeneratingTestCase()
  }
}

// 更新测试用例
const updateTestCase = (newTestCase: TestCase[]) => {
  emit('update:testCase', newTestCase)
}

// 获取选择题选项数据
const getMultipleChoiceOptions = () => {
  if (multipleChoiceTestRef.value) {
    return multipleChoiceTestRef.value.getOptions()
  }
  return []
}

// 获取判断题答案
const getJudgementAnswer = () => {
  if (judgementTestRef.value) {
    return judgementTestRef.value.getAnswer()
  }
  return true
}

const getJudgementAnalysis = () => {
  if (judgementTestRef.value) {
    return judgementTestRef.value.getAnalysis();
  }
  return "";
}

// 获取填空题答案
const getFillBlankAnswers = () => {
  if (fillBlankTestRef.value) {
    return fillBlankTestRef.value.getAnswers()
  }
  return []
}

const getFillBlankAnalysis = () => {
  if (fillBlankTestRef.value) {
    return fillBlankTestRef.value.getAnalysis();
  }
  return "";
}

// 获取综合题参考答案
const getComprehensiveAnswer = () => {
  if (comprehensiveTestRef.value) {
    return comprehensiveTestRef.value.getReferenceAnswer()
  }
  return ''
}

// 获取当前题型的答案数据
const getAnswerData = () => {
  switch (currentQuestionType.value) {
    case QuestionType.MULTIPLE_CHOICE:
      return {
        options: getMultipleChoiceOptions(),
        score: multipleChoiceTestRef.value ? multipleChoiceTestRef.value.getScore() : 0
      }
    case QuestionType.JUDGEMENT:
      return {
        isCorrect: getJudgementAnswer(),
        analysis: getJudgementAnalysis(),
        score: judgementTestRef.value ? judgementTestRef.value.getScore() : 0
      }
    case QuestionType.FILL_BLANK:
      return {
        answers: getFillBlankAnswers(),
        analysis: getFillBlankAnalysis(),
        score: fillBlankTestRef.value ? fillBlankTestRef.value.getScore() : 0
      }
    case QuestionType.COMPREHENSIVE:
      return {
        referenceAnswer: getComprehensiveAnswer(),
        score: comprehensiveTestRef.value ? comprehensiveTestRef.value.getScore() : 0
      }
    default:
      return {
        testCase: props.testCase
      }
  }
}

defineExpose({
  resetGeneratingTestCase,
  getAnswerData
})

watch(() => props.testCase, (newValue) => {
  emit('update:testCase', newValue || [])
}, {deep: true})
</script>

<template>
  <!-- 根据题目类型显示相应组件 -->
  <ProgrammingTest
      ref="programmingTestRef"
      v-if="showProgrammingContent"
      :test-case="testCase"
      :is-editor-fullscreen="isEditorFullscreen"
      @update:test-case="updateTestCase"
      @generate:test-case="handleGenerateTestCase"
  />

  <MultipleChoiceTest
      ref="multipleChoiceTestRef"
      v-if="showMultipleChoiceContent"
      :is-editor-fullscreen="isEditorFullscreen"
      :options="testCaseData.choiceOptionList"
      :score="testCaseData.score"
  />

  <JudgementTest
      ref="judgementTestRef"
      v-if="showJudgementContent"
      :is-editor-fullscreen="isEditorFullscreen"
      :is-correct="testCaseData.isCorrect"
      :analysis="testCaseData.analysis"
      :score="testCaseData.score"
  />

  <FillBlankTest
      ref="fillBlankTestRef"
      v-if="showFillBlankContent"
      :is-editor-fullscreen="isEditorFullscreen"
      :answers="testCaseData.answers"
      :analysis="testCaseData.analysis"
      :score="testCaseData.score"
  />

  <ComprehensiveTest
      ref="comprehensiveTestRef"
      v-if="showComprehensiveContent"
      :is-editor-fullscreen="isEditorFullscreen"
      :reference-answer="testCaseData.referenceAnswer"
      :score="testCaseData.score"
  />
</template>

<style scoped>
/* 移除原来的样式，因为已经移至各组件中 */
</style>
